package com.l.web.master.system.module;

import com.l.web.WebConstant;
import com.l.web.util.ControllerSupport;
import org.apache.log4j.Logger;
import org.l.qiao.Cluster;
import org.l.qiao.Page;
import org.l.qiao.annotation.Privilege;
import org.l.qiao.web.form.Form;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping(ModuleController.URI)
public class ModuleController extends ControllerSupport {

    public static final String URI = WebConstant.MASTER_URI + "/system/module";
    private static final Logger LOGGER = Logger.getLogger(ModuleController.class);
    @Autowired
    private ModuleService moduleService;

    @Privilege(module = "模块", function = "内容管理", operation = "查询")
    @RequestMapping("index.do")
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        return new ModelAndView(URI + "/index", modelMap);
    }

    @Privilege(module = "模块", function = "内容管理", operation = "查询")
    @RequestMapping("list.do")
    public ModelAndView list(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        Form form = getForm(request);
        Page page = form.getPage();
        List<Cluster> list = moduleService.getPageList(page);
        List<Cluster> exts = moduleService.getExts(list);
        modelMap.put("list", list);
        modelMap.put("page", page);
        modelMap.put("exts", exts);
        return new ModelAndView(URI + "/list", modelMap);
    }
//
//    @Privilege(module = "模块", function = "内容管理", operation = "详情")
//    @RequestMapping("detail.do")
//    public ModelAndView detail(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws IOException {
//        Form form = getForm(request);
//        String id = form.getString("id");
//
//        if (Validate.check(Validate.OBJECTID, id)) {
//            Module module = moduleService.getDetail(id);
//
//            if (module.isEmpty()) {
//                LOGGER.error("所查看的内容不存在");
//                response.sendError(404);
//                return null;
//            }
//
//            modelMap.put("module", module);
//            modelMap.put("check", ClusterUtil.getCheck(Module.class));
//        } else {
//            LOGGER.error("非法查看内容信息：id=" + id);
//            response.sendError(404);
//            return null;
//        }
//
//        return new ModelAndView(URI + "/detail", modelMap);
//    }
//
//    @Privilege(module = "模块", function = "内容管理", operation = "添加")
//    @RequestMapping("add.do")
//    public ModelAndView add(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
//        modelMap.put("check", ClusterUtil.getCheck(Module.class));
//        return new ModelAndView(URI + "/add", modelMap);
//    }
//
//    @Privilege(module = "模块", function = "内容管理", operation = "添加")
//    @RequestMapping("save.do")
//    public void save(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws IOException {
//        Form form = getForm(request);
//        Module module = new Module();
//        boolean result = form.getCluster(module);
//        LOGGER.debug("添加内容：" + module);
//
//        if (result) {
//            int code = moduleService.save(module);
//            ResponseInfo responseInfo = new ResponseInfo(code);
//            responseInfo.put("id", module.getId());
//            out(response, responseInfo);
//        } else {
//            response.sendError(404);
//            LOGGER.error("非法添加内容：" + module);
//        }
//    }
//
//    @Privilege(module = "模块", function = "内容管理", operation = "修改")
//    @RequestMapping("update.do")
//    public void update(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws IOException {
//        Form form = getForm(request);
//        KeyUpdate keyUpdate = form.getKeyUpdate(Module.class);
//        LOGGER.debug("修改信息信息：" + keyUpdate);
//        if (keyUpdate.check()) {
//            int code = ResponseCode.FAILURE;
//
//            try {
//                code = moduleService.update(keyUpdate);
//            } catch (Exception e) {
//                LOGGER.error("修改异常[" + keyUpdate + "]：" + e);
//            }
//
//            out(response, code);
//        } else {
//            LOGGER.error("非法修改：" + keyUpdate);
//            response.sendError(404);
//        }
//    }
//
//    @Privilege(module = "模块", function = "内容管理", operation = "删除")
//    @RequestMapping("save.do")
//    public void remove(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws IOException {
//        Form form = getForm(request);
//        List<String> ids = form.getList("id");
//        LOGGER.debug("删除内容：id=" + ids);
//
//        if (Validate.check(Validate.OBJECTID, ids)) {
//            int code = moduleService.remove(ids);
//            out(response, code);
//        } else {
//            LOGGER.error("非法删除内容：id=" + ids);
//            response.sendError(404);
//        }
//    }
}
